package com.mongo.aggregation.model.enums;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.io.Serializable;

/**
 * @author: qilongxin
 * @date: 2021/11/11 10:22
 */
public class ServerResponse<T> implements Serializable {

    private int code;

    private boolean result;

    @JsonProperty(defaultValue = "")
    private String msg;

    private T data;

    private boolean isSuccess() {
        return this.code == ResponseCode.SUCCESS.getCode();
    }

    private ServerResponse(int code) {
        this.code = code;
        this.result = this.isSuccess();
    }

    private ServerResponse(int code, T data) {
        this.code = code;
        this.result = this.isSuccess();
        this.data = data;
    }

    private ServerResponse(int code, String msg, T data) {
        this.code = code;
        this.result = this.isSuccess();
        this.msg = msg;
        this.data = data;
    }

    public static <T> ServerResponse<T> ok() {
        return new ServerResponse<>(ResponseCode.SUCCESS.getCode());
    }

    public  static <T> ServerResponse<T> ok(T data) {
        return new ServerResponse<>(ResponseCode.SUCCESS.getCode(), data);
    }

    public static <T> ServerResponse<T> ok(String msg, T data) {
        return new ServerResponse<>(ResponseCode.SUCCESS.getCode(), msg, data);
    }

    public static  <T> ServerResponse<T> fail() {
        return new ServerResponse<>(ResponseCode.FAIL.getCode());
    }

    public static  <T> ServerResponse<T> fail(T data) {
        return new ServerResponse<>(ResponseCode.FAIL.getCode(), data);
    }

    public static  <T> ServerResponse<T> fail(String msg, T data) {
        return new ServerResponse<>(ResponseCode.FAIL.getCode(), msg, data);
    }


    public int getCode() {
        return this.code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public boolean getResult() {
        return this.result;
    }

    public void setResult(boolean result) {
        this.result = result;
    }

    public String getMsg() {
        return this.msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
